package com.qkst.website.common.core;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.common.utils.SetEnums;
import com.qkst.website.web.bean.UserBean;

/**
 * @description : Controller基础类 集成该类后前后端可以方便使用base等参数
 * @Author : lihao
 * @Verion : 1.0v
 * @Email : ahlxlihao@163.com
 * @date 2017-04-28
 */
@Controller
public class BaseController {
	
	
	/**
	 * 前后台静态资源base路径
	 * @param request
	 * @return string
	 */
	@ModelAttribute("BASE")
	public String base(HttpServletRequest request) {
		return request.getContextPath()+"/static/common";
	}
	
	/**
	 * 后台静态资源base路径
	 * @param request
	 * @return string
	 */
	@ModelAttribute("BACKSTAGE")
	public String backstage(HttpServletRequest request) {
		return request.getContextPath()+"/static/backstage";
	}
	
	/**
	 * 前端官网静态资源base路径
	 * @param request
	 * @return string
	 */
	@ModelAttribute("WEB")
	public String web(HttpServletRequest request) {
		return request.getContextPath()+"/static/web";
	}
	
	/**
	 * 项目路径
	 * @param request
	 * @return string
	 */
	@ModelAttribute("web")
	public String webURL(HttpServletRequest request) {
		return request.getContextPath()+"/web";
	}
	
	/**
	 * 项目路径
	 * @param request
	 * @return string
	 */
	@ModelAttribute("backstage")
	public String backstageURL(HttpServletRequest request) {
		return request.getContextPath()+"/backstage";
	}
	
	protected void redirect(String result,HttpServletResponse response) throws IOException {
		response.sendRedirect(result);
	}
	
	/**
	 * 前端登录用户
	 * @param request
	 * @param response
	 * @return
	 */
	@ModelAttribute("_LOGIN_USER")
	public UserBean getLoginUser(HttpServletRequest request,HttpServletResponse response){
		UserBean user=(UserBean) request.getSession().getAttribute(SetEnums._LOGIN_USER.toString());
		if(null!=user){
			return user;
		}else{
			return new UserBean();
		}
	}
	
	/**
	 * 后端登录用户
	 * @param request
	 * @param response
	 * @return
	 */
	@ModelAttribute("_ADMIN_USER")
	public UserBean getAdminUser(HttpServletRequest request,HttpServletResponse response){
		UserBean user=(UserBean) request.getSession().getAttribute(SetEnums._ADMIN_USER.toString());
		if(null!=user){
			return user;
		}else{
			return new UserBean();
		}
	}
	
	/**
	 * 跳转后台
	 * @param path
	 * @return ModelAndView
	 * @throws IOException
	 */
	protected ModelAndView backstageModelAndView( String path) throws IOException {
		ModelAndView mv = new ModelAndView("backstage/"+path);
		return mv;
	}
	/**跳转前端
	 * @param path
	 * @return ModelAndView
	 * @throws IOException
	 */
	protected ModelAndView webModelAndView( String path) throws IOException {
		ModelAndView mv = new ModelAndView("web/"+path);
		return mv;
	}
	
	
	
	
	
}
